package com.ok.integral.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ok.common.utils.UserContext;
import com.ok.integral.domain.po.OkIntegral;
import com.ok.integral.mapper.OkIntegralMapper;
import com.ok.integral.service.IOkIntegralService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.reflections.util.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 积分表 服务实现类
 * </p>
 *
 * @author lhw
 * @since 2024-05-06
 */
@Service
@RequiredArgsConstructor
public class OkIntegralServiceImpl extends ServiceImpl<OkIntegralMapper, OkIntegral> implements IOkIntegralService {


    @Override
    @Transactional
    public void add(Long userId) {
        OkIntegral okIntegral = new OkIntegral();
        okIntegral.setUserId(userId);
        this.save(okIntegral);
    }

    @Override
    @Transactional
    public void updateWith(Double integral) {
        LambdaQueryWrapper<OkIntegral> queryWrapper = new LambdaQueryWrapper<>();
        //得到用户id
        Long userId = UserContext.getUser();
        //根据用户id的到对应积分信息
        queryWrapper.eq(!Utils.isEmpty(String.valueOf(userId)),OkIntegral::getUserId,userId);
        OkIntegral okIntegral = this.getOne(queryWrapper);

        //修改用户积分
        okIntegral.setIntegral(okIntegral.getIntegral()+integral);

        //TODO 发送异步请求，修改用户表中的积分信息

        this.updateById(okIntegral);

    }


}
